package acwing._1_1AlgorithmBasic._3SearchGraph;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._3SearchGraph
 * @File : Q0845.java
 * @Author : WangRuoyu
 * @Date : 2023/4/3 16:40
 */

import java.io.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

public class Q0845 {

    static boolean check(int[] d) {
        for (int i = 0; i < 9; ++i) {
            if ((d[i] + 8) % 9 != i) {
                return false;
            }
        }
        return true;
    }

    static String changeStr(int[] d) {
        StringBuilder sb = new StringBuilder(9);
        for (int i = 0; i < 9; ++i) {
            sb.append(d[i]);
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");
        int[] d = new int[11];

        for (int i = 0; i < 9; ++i) {
            if ("x".equals(words[i])) {
                d[9] = i;
            } else {
                d[i] = Integer.parseInt(words[i]);
            }
        }

        Queue<int[]> q = new LinkedList<>();
        HashSet<String> set = new HashSet<>();
        int[] dir = {3, -3, 1, -1};

        q.add(d);
        set.add(changeStr(d));

        while (!q.isEmpty()) {
            int[] point = q.poll();
            if (check(point)) {
                System.out.println(point[10]);
                return;
            }
            int idx = point[9];
            for (int i = 0; i < 4; ++i) {
                if ((idx % 3 == 0 && i == 3) || (idx % 3 == 2 && i == 2)) {
                    continue;
                }
                int newIdx = idx + dir[i];
                if (newIdx >= 0 && newIdx <= 8) {
                    int[] newD = new int[11];
                    System.arraycopy(point, 0, newD, 0, 11);
                    newD[newIdx] = point[idx];
                    newD[idx] = point[newIdx];
                    newD[9] = newIdx;
                    newD[10] = point[10] + 1;
                    String str = changeStr(newD);
                    if (!set.contains(str)) {
                        q.add(newD);
                        set.add(str);
                    }
                }
            }
        }
        System.out.println(-1);
    }
}

